package com.zwface.demo.greendao;

import com.zwface.demo.App;
import com.zwface.demo.model.db.Course;
import com.zwface.demo.model.db.JoinUserWithCourse;
import com.zwface.demo.model.db.UserInfo;
import com.zwface.demo.utils.SharedPreferenceUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * created by      张中文
 * on:             2020/4/27 17:29
 * 描述：课程信息数据库操作
 */
public class CourseManager
{
    static CourseDao courseDao = App.getInstance().getDaoSession().getCourseDao();

    public static Long addCourse(Course course){
        return courseDao.insertOrReplace(course);
    }

    public static void updateCourse(Course course){
        courseDao.update(course);
    }

    public static void deleteCourse(Long courseId){
        courseDao.deleteByKey(courseId);
    }

    public static List<Course> queryAllCourse(){
        return courseDao.queryBuilder().build().list();
    }


    /**
     * 根据课程id查询课程信息
     * @param courseId
     * @return
     */
    public static Course queryCourseById(Long courseId){
        Course course = courseDao.queryBuilder().where(CourseDao.Properties.Id.eq(courseId)).build().unique();
        return course;
    }

    /**
     * 根据时间查询课程信息
     * @param
     * @return
     */
    public static Course queryCourseByTime(int week,int section){
        Course course = courseDao.queryBuilder().where(CourseDao.Properties.Week.eq(week),
                CourseDao.Properties.Section.eq(section)).build().unique();
        return course;
    }

    /**
     * 获取所有课程数据（如果数据库中没有课程的话，显示默认课程）
     * @return
     */
    /*public static List<Course>[] getCourseData() {

        String userId = SharedPreferenceUtils.getParam(App.getInstance().getApplicationContext(),SharedPreferenceUtils.USER_ID);
        List<Course> courseModels[] = new ArrayList[7];

        for (int i = 0; i < courseModels.length; i++) {
            courseModels[i] = new ArrayList<>();
        }

        List<Course> dbList = queryAllCourse();
        if(dbList != null && dbList.size() > 0){
            //显示数据库数据
            List<Course> models_1 = new ArrayList<>();
            List<Course> models_2 = new ArrayList<>();
            List<Course> models_3 = new ArrayList<>();
            List<Course> models_4 = new ArrayList<>();
            List<Course> models_5 = new ArrayList<>();
            List<Course> models_6 = new ArrayList<>();
            List<Course> models_7 = new ArrayList<>();
            for (Course course:dbList) {
                switch (course.getWeek()){
                    case 1:
                        models_1.add(course);
                        break;
                    case 2:
                        models_2.add(course);
                        break;
                    case 3:
                        models_3.add(course);
                        break;
                    case 4:
                        models_4.add(course);
                        break;
                    case 5:
                        models_5.add(course);
                        break;
                    case 6:
                        models_6.add(course);
                        break;
                    case 7:
                        models_7.add(course);
                        break;
                }
            }
            courseModels[0].addAll(models_1);
            courseModels[1].addAll(models_2);
            courseModels[2].addAll(models_3);
            courseModels[3].addAll(models_4);
            courseModels[4].addAll(models_5);
            courseModels[5].addAll(models_6);
            courseModels[6].addAll(models_7);

        }else{
            //显示默认数据
            List<Course> models_1 = new ArrayList<>();
            models_1.add(new Course(0l, "C语言", 1, 1, 1, "A401", (int) (Math.random() * 10)));
            models_1.add(new Course(1l, "Ruby", 3, 1, 1, "A453", (int) (Math.random() * 10)));
            models_1.add(new Course(2l, "PHP", 6, 1, 1, "A483", (int) (Math.random() * 10)));
            courseModels[0].addAll(models_1);

            List<Course> models_2 = new ArrayList<>();
            models_2.add(new Course(3l, "Swift", 2, 1, 2, "A222", (int) (Math.random() * 10)));
            models_2.add(new Course(4l, "JavaScript", 6, 1, 2, "A777", (int) (Math.random() * 10)));
            courseModels[1].addAll(models_2);

            List<Course> models_3 = new ArrayList<>();
            models_3.add(new Course(5l, "Python", 1, 1, 3, "A342", (int) (Math.random() * 10)));
            models_3.add(new Course(6l, "Visual Basic .NET", 5, 1, 3, "A737", (int) (Math.random() * 10)));
            courseModels[2].addAll(models_3);

            List<Course> models_4 = new ArrayList<>();
            models_4.add(new Course(7l, "C#", 1, 1, 4, "A666", (int) (Math.random() * 10)));
            models_4.add(new Course(8l, "R语言", 5, 1, 4, "A888", (int) (Math.random() * 10)));
            models_4.add(new Course(9l, "Java", 9, 1, 4, "A828", (int) (Math.random() * 10)));
            courseModels[3].addAll(models_4);

            List<Course> models_5 = new ArrayList<>();
            models_5.add(new Course(10l, "Android", 1, 1, 5, "A466", (int) (Math.random() * 10)));
            models_5.add(new Course(11l, "Groovy", 3, 1, 5, "A434", (int) (Math.random() * 10)));
            models_5.add(new Course(12l, "Objective-C", 6, 1, 5, "A411", (int) (Math.random() * 10)));
            courseModels[4].addAll(models_5);

            List<Course> models_6 = new ArrayList<>();
            models_6.add(new Course(13l, "C++", 1, 1, 6, "A422", (int) (Math.random() * 10)));
            models_6.add(new Course(14l, "SQL", 5, 1, 6, "A402", (int) (Math.random() * 10)));
            courseModels[5].addAll(models_6);

            //把默认数据添加到数据库中

            List<JoinUserWithCourse> joinUserWithCourses = new ArrayList<>();
            for (List<Course> courses : courseModels){
                for (Course course:courses) {
                    //插入课程表信息
                    Long courseId = addCourse(course);

                    //设置用户-课程关联表信息
                    JoinUserWithCourse joinUserWithCourse = new JoinUserWithCourse();
                    joinUserWithCourse.setCId(courseId);
                    joinUserWithCourse.setUId(Long.parseLong(userId));
                    joinUserWithCourses.add(joinUserWithCourse);
                }
            }
            //插入用户-课程关联表信息
            JoinUserWithCourseManager.addJoinUserWithCoutses(joinUserWithCourses);
        }

        return courseModels;

    }*/

    /**
     * 获取用户关联的所有课程
     * @param userNo
     * @return
     */
    public static List<Course>[] getCoursesByUserNo(String userNo){
        UserInfo userInfo = UserInfoManager.queryUserByNo(userNo);
        if(userInfo == null)
            return null;

        //当添加、更新或删除很多关联实体时可以使用reset方法来清掉缓存,否则getCourses获取不到数据
        userInfo.resetCourses();
        List<Course> courseModels[] = new ArrayList[7];
        List<Course> dbList = userInfo.getCourses();
        if(dbList == null || dbList.size() == 0){
            for (int i = 0; i < courseModels.length; i++) {
                courseModels[i] = new ArrayList<>();
            }
            return courseModels;
        }
        //显示数据库数据
        List<Course> models_1 = new ArrayList<>();
        List<Course> models_2 = new ArrayList<>();
        List<Course> models_3 = new ArrayList<>();
        List<Course> models_4 = new ArrayList<>();
        List<Course> models_5 = new ArrayList<>();
        List<Course> models_6 = new ArrayList<>();
        List<Course> models_7 = new ArrayList<>();
        for (Course course:dbList) {
            switch (course.getWeek()){
                case 1:
                    models_1.add(course);
                    break;
                case 2:
                    models_2.add(course);
                    break;
                case 3:
                    models_3.add(course);
                    break;
                case 4:
                    models_4.add(course);
                    break;
                case 5:
                    models_5.add(course);
                    break;
                case 6:
                    models_6.add(course);
                    break;
                case 7:
                    models_7.add(course);
                    break;
            }
        }

        for (int i = 0; i < courseModels.length; i++) {
            courseModels[i] = new ArrayList<>();
        }
        courseModels[0].addAll(models_1);
        courseModels[1].addAll(models_2);
        courseModels[2].addAll(models_3);
        courseModels[3].addAll(models_4);
        courseModels[4].addAll(models_5);
        courseModels[5].addAll(models_6);
        courseModels[6].addAll(models_7);

        return courseModels;
    }

}



















